# G1 [X<pos>] [Y<pos>] [Z<pos>] [E<pos>] [F<speed>]
# M106                                                      # Fan On
# M107                                                      # Fan Off
# M117 <message>
# M112                                                      # Emergency stop
# M114                                                      # Get current position
# M115                                                      # Get firmware version
# M300                                                      # Play beep sound
# M400                                                      # Wait for current moves to finish
# M900                                                      # Set Linear Advance Scaling Factors
# SET_DUAL_CARRIAGE CARRIAGE=[0|1]
# SET_GCODE_OFFSET [X=<value>] [Y=<value>] [Z=<value>]


# Helper script to park the carriage (called from T0 and T1 macros)
[gcode_macro PARK_extruder0]
gcode:
    SAVE_GCODE_STATE NAME=park0
    G90
    G1 X{printer.configfile.settings.stepper_x.position_min+10} F12000
    RESTORE_GCODE_STATE NAME=park0

# Activate the primary extruder
[gcode_macro T0]
gcode:
#    {% if printer.dual_carriage.active_carriage == "CARRIAGE_1" %}
        PARK_extruder1
#    {% endif %}
#    {% set fan_speed = printer["gcode_macro M106"].swap_speed %}
#    {% if fan_speed != -1 %}
#        SET_FAN_SPEED FAN=fan_extruder SPEED={fan_speed}
#    {% else %}
#        # Update core Klipper's fan speed to the fan speed of the active toolhead
#        # Only do this if you have a sacrificial [fan] section
#        M106.1 S{printer["fan_generic fan_extruder"].speed * 255}
#    {% endif %}
    ACTIVATE_EXTRUDER EXTRUDER=extruder
    SAVE_VARIABLE VARIABLE=current_extruder VALUE=0
    SET_DUAL_CARRIAGE CARRIAGE=0
    SET_GCODE_OFFSET X=0 Y=0

[gcode_macro PARK_extruder1]
gcode:
    SAVE_GCODE_STATE NAME=park1
    G90
    G1 X{printer.configfile.settings.dual_carriage.position_max-10} F12000
    RESTORE_GCODE_STATE NAME=park1

[gcode_macro T1]
gcode:
#    {% if printer.dual_carriage.active_carriage != "CARRIAGE_0" %}
        PARK_extruder0
#    {% endif %}
#    {% set fan_speed = printer["gcode_macro M106"].swap_speed %}
#    {% if fan_speed != -1 %}
#        SET_FAN_SPEED FAN=fan_extruder1 SPEED={fan_speed}
#    {% else %}
#        # Update core Klipper's fan speed to the fan speed of the active toolhead
#        # Only do this if you have a sacrificial [fan] section
#        M106.1 S{printer["fan_generic fan_extruder1"].speed * 255}
#    {% endif %}
    ACTIVATE_EXTRUDER EXTRUDER=extruder1
    SAVE_VARIABLE VARIABLE=current_extruder VALUE=1
    SET_DUAL_CARRIAGE CARRIAGE=1
    SET_GCODE_OFFSET X=0 Y=0


################################################################################
################################################################################

[gcode_macro CANCEL_PRINT]
rename_existing: BASE_CANCEL_PRINT
gcode:
    TURN_OFF_HEATERS
    CLEAR_PAUSE
    SDCARD_RESET_FILE
    BASE_CANCEL_PRINT

[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
gcode:
    ##### set defaults #####
    {% set x = params.X|default(230) %}      #edit to your park position
    {% set y = params.Y|default(230) %}      #edit to your park position
    {% set z = params.Z|default(10)|float %} #edit to your park position
    {% set e = params.E|default(1) %}        #edit to your retract length
    ##### calculate save lift position #####
    {% set max_z = printer.toolhead.axis_maximum.z|float %}
    {% set act_z = printer.toolhead.position.z|float %}
    {% set lift_z = z|abs %}
    {% if act_z < (max_z - lift_z) %}
        {% set z_safe = lift_z %}
    {% else %}
        {% set z_safe = max_z - act_z %}
    {% endif %}
    ##### end of definitions #####
    SAVE_GCODE_STATE NAME=PAUSE_state
    BASE_PAUSE
    G91
    G1 E-{e} F2100
    G1 Z{z_safe}
    G90
    G1 X{x} Y{y} F6000


[gcode_macro RESUME]
rename_existing: BASE_RESUME
gcode:
    ##### set defaults #####
    {% set e = params.E|default(1) %} #edit to your retract length
    G91
    G1 E{e} F2100
    G90
    RESTORE_GCODE_STATE NAME=PAUSE_state MOVE=1
    BASE_RESUME

################################################################################
################################################################################
